import moment from 'moment';

export function nowDate(str) {
  return moment().locale('zh-cn').format(str);
}

export function currentDate(time) {
  let date = null
  if (time) {
    date = new Date(time)
  } else {
    date = new Date()
  }
  let year = date.getFullYear()
  let month = date.getMonth() + 1
  let day = date.getDate()
  if (month < 10) {
      month = '0' + month
  }
  if (day < 10) {
    day = '0' + day
  }
  return year + '-' + month + '-'+day
}

/* 获取前一天日期 */
export function getPreDate(time) {
  let pre = new Date(time).getTime() - 3600 * 1000 * 24
  return currentDate(pre)
}

/**
 * 时间比较
 */
export function compareDate(start, end) {
  return new Date(end).getTime() < new Date(start).getTime()
}

/**
 * 2个日期的天数差
 */
export function DateDiffDay(Date1, Date2) {
  if(new Date(Date1).getTime() < new Date(Date2).getTime()){
    return 0
  }
  return parseInt(Math.abs(new Date(Date1) - new Date(Date2)) / 1000 / 60 / 60 / 24); //把差的毫秒数转换为天数
}

/**
 * 2个日期的小时差(保留6位小数)
 */
export function DateDiffHour(Date1, Date2) {
  if(new Date(Date1).getTime() < new Date(Date2).getTime()){
    return 0
  }
  let diff = parseFloat(Math.abs(new Date(Date1) - new Date(Date2)) / 1000 / 60 / 60); //把差的毫秒数转换为小时数
  return Math.round(diff*1000000)/1000000
}
